package com.mes.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mes.entity.User;
import com.mes.service.InspectionSubDataService;
import com.yy.base.page.Message;
import com.yy.base.util.UserUtil;


/**
 * 巡检
 * @author xgh
 */
@Controller
@RequestMapping("inspectionSubData")
public class InspectionSubDataController {
	@Autowired
	private InspectionSubDataService inspectionSubDataService;
	
	//加载工序信息
	@ResponseBody
	@RequestMapping(value="initProcessInfo",method=RequestMethod.POST)
	public Message initProcessInfo(HttpServletRequest request){
		Message msg = new Message();
		User user = (User)request.getSession().getAttribute(UserUtil.USERSESSION);
		String orgId = (String)request.getSession().getAttribute("orgId");
		msg.setPageList(inspectionSubDataService.getProcessList(orgId));
		msg.put("LoginName", user.getName());
		List<Map<String,Object>> orgList = inspectionSubDataService.getDutyOrgList(orgId);//获得机构列表
		List<Map<String,Object>> manList = inspectionSubDataService.getDutyManList(orgId);//获得责任人列表
		List<Map<String,Object>> resultList = inspectionSubDataService.getResultList(orgId);//获得后果列表
		msg.put("orgList", orgList);
		msg.put("manList", manList);
		msg.put("resultList", resultList);
		return msg;
	}
	//加载工序巡检要求项目
	@ResponseBody
	@RequestMapping(value="getInspectionList",method=RequestMethod.POST)
	public Message getInspectionList(String processId,HttpServletRequest request){
		String orgId = (String)request.getSession().getAttribute("orgId");
		Message msg = new Message();
		msg.put("inspectionList", inspectionSubDataService.getInspectionList(processId));
		msg.put("inspectionRsList", inspectionSubDataService.getInspectionRsList(processId));
		msg.put("inspectionLogList",inspectionSubDataService.searchInspectionLogList(orgId,processId));//查询出当前巡检历史记录
		return msg;
	}
	
	//提交巡检记录
	@ResponseBody
	@RequestMapping(value="saveInspectionData",method=RequestMethod.POST)
	public Message saveInspectionData(HttpServletRequest request,String processId,String itemData){
		Message msg = new Message();
		User user = (User)request.getSession().getAttribute(UserUtil.USERSESSION);
		String orgId = (String)request.getSession().getAttribute("orgId");
		String result = inspectionSubDataService.saveInspectionData(processId,itemData,orgId,user);
		if("YES".equals(result)) {
			msg.setOkMessage("保存成功!");
		} else if("NO".equals(result)) {
			msg.setErrorMessage("保存失败!");
		} else {
			msg.setErrorMessage(result);
		}
		return msg;
	}
	
	//巡检记录列表
	@PostMapping("getInspectionLogList")
	@ResponseBody
	public List<Map<String,Object>> getInspectionLogList(HttpServletRequest request){
//		User user = (User)request.getSession().getAttribute(UserUtil.USERSESSION);
		String orgId = (String)request.getSession().getAttribute("orgId");
		return inspectionSubDataService.getInspectionLogList(orgId);
	}

}
